package com.easy.controller;

import com.easy.bean.Admin;
import com.easy.service.LoginService;
import com.easy.util.*;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Random;

@RestController
@RequestMapping("/login")
@CrossOrigin

public class LoginController {
    @Autowired
    LoginService loginService;

    @GetMapping("/captcha")
    public Result captcha() {
        return Result.success(CaptchaUtil.getCaptcha());
    }

    @PostMapping("/dologin")
    public Result login(Admin admin, @RequestParam("captcha") String captcha, @RequestParam("key") String key) {
        //验证码验证
        if (!CaptchaUtil.checkCaptcha(key, captcha)) {
            return Result.fail(500, "验证码错误");
        }
        //登录
        try {
            String token = loginService.login(admin);
            //删除掉验证码
            return Result.success("登陆成功", token);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail(500, e.getMessage());
        }

    }

    @GetMapping("/logininfo")
    public Result logininfo(HttpServletRequest request) {
        String token = request.getHeader(SYS.TOKEN_AUTHORIZATION);
        Admin admin = loginService.logininfo(token);
        return Result.success(admin);
    }

    @PutMapping("/resetPassword")
    public Result resetPassword(HttpServletRequest request,  Admin admin) {
        // 1. 从请求头拿token，获取当前登录用户
        String token = request.getHeader(SYS.TOKEN_AUTHORIZATION);
        Admin loginUser = loginService.logininfo(token);
        if (loginUser == null) {
            return Result.fail(500, "登录已过期，请重新登录");
        }

        // 2. 直接用当前登录用户的信息，不需要前端传member比对
        // 3. 加密新密码并更新
        System.out.println("前端传来的密码是"+admin.getPassword());
        loginUser.setPassword(PasswordUtil.getEncryption(admin.getPassword()));
        loginService.edit(loginUser); // 更新密码

        return Result.success(200,"修改成功",null);
    }

    @PostMapping("/sendVerificationCode")
    public Result sendVerificationCode(@RequestParam("email") String email) {
        Random random = new Random();
        System.out.println(email);

        // 生成6位随机数字（范围：100000-999999）
        Integer text = random.nextInt(900000) + 100000;
        try {
            EmailUtil.sendEmail(email, text.toString());
            EmailUtil.setCode(email, text.toString());
            return Result.success(200, "发送成功", null);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail(500, "发送失败: " + e.getMessage());
        }
    }
    @PostMapping("verifyCode")
    public Result verifyCode(@RequestParam("email") String email,@RequestParam("code") String code){
        System.out.println("验证码为"+code);
        System.out.println("验证邮箱为"+email);
        if(code.equals(EmailUtil.getCode(email))){
            return Result.success("验证成功");
        }else{
            return Result.fail(500,"验证码错误");
        }
    }
}
